package cn.wolfcode.wolf2w.web.controller;

import cn.wolfcode.wolf2w.domain.Banner;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.query.BannerQuery;
import cn.wolfcode.wolf2w.service.IBannerService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;


@Controller
@RequestMapping("banner")
public class BannerController {
    @Autowired
    private IBannerService bannerService;

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private ITravelService travelService;

    /**
     * 分页查询
     * @param model
     * @param qo
     * @return
     */
    @RequestMapping("list")
    public String list(Model model,@ModelAttribute("qo") BannerQuery qo){
        // 分页查询
        Page page = bannerService.query(qo);
        // 共享数据
        model.addAttribute("page",page);
        return "banner/list" ;
    }

    /**
     * 通过选择类型（攻略/游记）获取对应信息
     * @return
     */
    @RequestMapping("getAllType")
    @ResponseBody
    public Object getAllType(){

        HashMap<String, Object> map = new HashMap<>();
        // 查询所有的攻略明细
        List<Strategy> strategies = strategyService.list();
        // 查询所有游记
        List<Travel> travelList = travelService.list();
        map.put("sts",strategies);
        map.put("ts",travelList);
        return JsonResult.success(map);
    }

    /**
     * 添加与编辑
     * @param banner
     * @return
     */
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Banner banner){

        bannerService.saveOrUpdate(banner);

        return JsonResult.success();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public Object delete(String id){
        bannerService.delete(id);
        return JsonResult.success();
    }



}
